# This sample code supports Appium Python client >=2.3.0
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
import time

from appium import webdriver
from appium.options.common.base import AppiumOptions
from appium.webdriver.common.appiumby import AppiumBy

# For W3C actions
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput


options = AppiumOptions()
options.load_capabilities({
	"appium:deviceName": "127.0.0.1:7555",
	"platformName": "Android",
	"appium:platformVersion": "12",
	"appium:appPackage": "com.sina.weibo",
	"appium:appActivity": "com.sina.weibo.SplashActivity",
	"appium:ensureWebviewsHavePages": True,
	"appium:nativeWebScreenshot": True,
	"appium:newCommandTimeout": 3600,
	"appium:connectHardwareKeyboard": True
})

driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", options=options)

driver.implicitly_wait(10)
time.sleep(5)

def click(x, y):
	actions = ActionChains(driver)
	actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
	actions.w3c_actions.pointer_action.move_to_location(x, y)
	actions.w3c_actions.pointer_action.pointer_down()
	actions.w3c_actions.pointer_action.pause(0.1)
	actions.w3c_actions.pointer_action.release()
	time.sleep(3)
	actions.perform()
	driver.implicitly_wait(10)
	time.sleep(3)


def duanxin_login(phone, sms):
    el3 = driver.find_element(by=AppiumBy.ID, value="com.sina.weibo:id/et_login_view_phone")
    el3.send_keys(phone)

    click(797, 382)

    el5 = driver.find_element(by=AppiumBy.ID, value="com.sina.weibo:id/et_login_view_sms")
    el5.send_keys(sms)

    click(443, 516)
    click(467, 519)


def mima_login(phone, sms):
	click(449, 1450)

	# 修正缩进，使用4个空格
	el3 = driver.find_element(by=AppiumBy.ID, value="com.sina.weibo:id/et_login_view_uname")
	el3.send_keys(phone)
	el4 = driver.find_element(by=AppiumBy.ID, value="com.sina.weibo:id/et_login_view_psw")
	el4.send_keys(sms)

	click(452, 513)
	# 点击阅读同意按钮
	click(183, 1511)
	# 点击登录
	click(461, 510)
	# 点击取消按钮
	click(382, 876)

#滑动-切换视频
def slide(x1, y1, x2, y2):
	actions = ActionChains(driver)
	actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
	actions.w3c_actions.pointer_action.move_to_location(x1, y1)
	actions.w3c_actions.pointer_action.pointer_down()
	actions.w3c_actions.pointer_action.move_to_location(x2, y2)
	actions.w3c_actions.pointer_action.release()
	time.sleep(3)
	actions.perform()
	time.sleep(3)

click(534, 959)

actions = ActionChains(driver)
actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
actions.w3c_actions.pointer_action.move_to_location(449, 198)
time.sleep(20)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.pause(0.1)
actions.w3c_actions.pointer_action.release()
actions.perform()
time.sleep(10)


el1 = driver.find_element(by=AppiumBy.ID, value="com.sina.weibo:id/tv_search_keyword")
time.sleep(3)
el1.send_keys("中国")
actions = ActionChains(driver)
actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
actions.w3c_actions.pointer_action.move_to_location(220, 137)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.pause(0.1)
actions.w3c_actions.pointer_action.release()
actions.perform()

click(440, 153)

#点击返回按钮
click(27, 70)

#点击取消按钮
click(867, 67)

click(864, 76)

duanxin_login("1234567891", "")
duanxin_login("12345678910", "")
duanxin_login("123456789101", "")

duanxin_login("10987654321", "12345")
duanxin_login("10987654322", "123456")
duanxin_login("10987654323", "1234567")

duanxin_login("然后表内佛人防反反复复", "123456")
duanxin_login("???@#", "123456")

mima_login("12345678910", "123456")

#点击取消按钮返回
click(46, 70)

#点击视频按钮
click(278, 1551)

#滑动距离小，无法切换下一个视频
slide(418, 1017,431, 785)

#正常切换下一个视频
slide(357, 1142,366, 577)

#往上滑，切换上一个视频
slide(357, 577,366, 1142)

driver.quit()